import Vue from 'vue';
import Resource from 'vue-resource';

import { EventEmitter } from 'events';

Vue.use(Resource);

var store = new EventEmitter();
var apiOrigin = process.env.NODE_ENV === 'production'
	? 'https://manager.lemonce.net/' // api for prod here
	: 'http://127.0.0.1:81/'; // api for dev here

store.getTaskList = async function (projectId) {
	var resource = Vue.resource(apiOrigin + 'public/task{?projectId, mask}');
	var mask = 'schemas';
	var res = await resource.get({projectId, mask});

	var taskList = res.data;
	taskList.forEach(function (t) {
		try {
			t.schemas = JSON.parse(t.schemas);
		} catch (e) {
			t.schemas = null;
		}
	});

	return taskList;
};

store.getTask = async function (taskId) {
	var resource = Vue.resource(apiOrigin + 'public/task{/taskId}');

	var res = await resource.get({taskId});

	var t = res.data;
	try {
		t.schemas = JSON.parse(t.schemas);
	} catch (e) {
		t.schemas = null;
	}

	return t;
};

store.getResultList = async function (projectId, taskId) {
	var resource = Vue.resource(apiOrigin + 'public/result{?projectId,task}');

	var res = await resource.get({projectId, task: taskId});

	return res.data;
};

module.exports = store;
